package com.anshi.shopchangepro.selfview;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.anshi.shopchangepro.R;
import com.anshi.shopchangepro.frag.CommentFrag;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class DynamicImgAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> imgs = new ArrayList<String>();
    private int itemLayoutRes;
    private Dialog dialog;
    private ImageView iiImg;
    private CommentFrag commentFrag;
    @SuppressLint("ResourceType")
    private List<ImageView> images =new ArrayList<>();
    public DynamicImgAdapter(Context mContext, List<String> imgs, int itemLayoutRes, CommentFrag commentFrag) {
        this.mContext = mContext;
        this.imgs = imgs;
        this.itemLayoutRes = itemLayoutRes;
        this.commentFrag = commentFrag;
        dialog = new Dialog(mContext, R.style.FullActivity);
        WindowManager.LayoutParams attributes = commentFrag.getActivity().getWindow().getAttributes();
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        attributes.height = WindowManager.LayoutParams.MATCH_PARENT;
        dialog.getWindow().setAttributes(attributes);
        Log.e("22222 imgs",imgs.size()+"");
        for (int i = 0; i < imgs.size(); i++) {
            images.add(getImageView(imgs.get(i)));
        }
    }
    @Override
    public int getCount() {
            if (null != imgs) {
                return imgs.size();
            }
            return 0;
    }

    @Override
    public Object getItem(int i) {
        if (null != imgs) {
            return imgs.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view= LayoutInflater.from(mContext).inflate(itemLayoutRes, null);
            holder=new ViewHolder();
            holder.ivCommentImg=view.findViewById(R.id.iv_dynamic_img);
            view.setTag(holder);
        }else {
            holder=(ViewHolder)view.getTag();
        }
        iiImg=view.findViewById(R.id.iv_dynamic_img);
        Glide.with(mContext)
                .load(imgs.get(i))
                .override(300, 300)
                .into(holder.ivCommentImg);
        images.add(getImageView(imgs.get(i)));
        iiImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.setContentView(images.get(i));
                dialog.show();
            }
        });
        //大图的点击事件（点击让他消失）
        images.get(i).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        return view;
    }
    class ViewHolder{
        ImageView ivCommentImg;
    }
    private ImageView getImageView(String img) {
        ImageView imageView = new ImageView(mContext);
        //宽高
        imageView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        Glide.with(mContext).load(img).into(imageView);
        return imageView;
    }

}
